/**
 * 启动系统
 */
var ZGIT = {
	UNAME: '',
	UINIT: false,
	ON_NET: true,

	// URL_BASE: 'http://localhost:8088',
	URL_BASE: 'http://meta.coding.io',

	log: function(msg) {
		try {
			console.log(msg);
		} catch(e) {
		}
	},

	/* 初始用户 */
	initConfigs: function() {
		// ZGIT.UNAME = window.localStorage.getItem("UNAME") || "";
		// var init = window.localStorage.getItem("UINIT") || "FALSE";
		// ZGIT.UINIT = (init.toUpperCase() === "TRUE");

		ZStore.getItem("UNAME", function(result) {
			if(result.success) {
				ZGIT.UNAME = result.value;
			}
		});

		ZStore.getItem("UINIT", function(result) {
			if(result.success) {
				ZGIT.UINIT = (result.value.toUpperCase() === "TRUE");
			}
		});
	},

	/* 首次登陆 */
	setUserInit: function() {
		// window.localStorage.setItem("UINIT", "TRUE");

		ZStore.setItem("UINIT", "TRUE", function(result) {
			// nothing to do
		});
	},

	/* 设置用户 */
	setUserName: function(uname) {
		ZGIT.UNAME = uname;
	},

	/* 存储用户 */
	saveUserName: function(save) {
		if(!save) {
			// window.localStorage.setItem("UNAME", "");
			ZStore.setItem("UNAME", "", function(result) {
				// nothing to do
			});
		} else {
			// window.localStorage.setItem("UNAME", ZGIT.UNAME);
			ZStore.setItem("UNAME", ZGIT.UNAME, function(result) {
				// nothing to do
			});
		}
	}
};

ionic.Platform.ready(function() {
	// console.log("系统启动完成，应用平台: " + ionic.Platform.platforms);
	// document.addEventListener("pause", onAppPause, false);
	document.addEventListener("online", onOnline, false);
	document.addEventListener("offline", onOffline, false);

	/* 退出应用 */
	var onAppPause = function() {
		alert("退出系统");
	};

	/* 网络在线 */
	var onOnline = function() {
		ZGIT.ON_NET = true;
	};

	/* 网络掉线 */
	var onOffline = function() {
		ZGIT.ON_NET = false;
	};

	/* 读取配置参数 */
	try {
		ZGIT.initConfigs();
	} catch(e) {
		ZGIT.log(e);
	}
});

/* 通用错误处理 */
window.onerror = function(msg, url, line) {
	var idx = url.lastIndexOf("/");
	if(idx > -1) {
		url = url.substring(idx + 1);
	}
	alert("ERROR in " + url + " (line #" + line + "): \n" + msg);
	return false;
};

/**
 * APP配置
 */
angular.module('ZIT', ['ionic', 'ngRoute'])
/* BR-参数值 */
// .value('URL_BASE', 'http://localhost:8088')
// .value('URL_BASE', 'https://node-etom.rhcloud.com')
/* BR-常量值 */
.constant('$ionicLoadingConfig', {
	template: '<i class="ion ion-loading-a"></i> 努力加载数据中……'
})

/* BR-根节点 */
.run(function($rootScope, $location) {

	$rootScope.isLogined = function() {
		return(ZGIT.UNAME.length > 1);
	};

	$rootScope.gotoUrl = function(uri) {
		$location.path(uri);
	};

	$rootScope.gotoLogin = function() {
		$location.path('/user-login');
	};

	$rootScope.gotoHome = function() {
		$location.path('/index-home');
	};
	$rootScope.gotoClub = function() {
		$location.path('/club-home');
	};
	$rootScope.gotoMine = function() {
		$location.path('/mine-home');
	};

	$rootScope.gotoAboutXWei = function() {
		$location.path('/about-xwei');
	};
	$rootScope.gotoCheckUpdate = function() {
		alert("检查更新");
	};

	$rootScope.doLogout = function() {
		ZGIT.setUserName('');
		ZGIT.saveUserName(false);
		$location.path('/user-login');
	};
})

/* BR-HTTP服务 */
.factory('UserDAO', function(URL_BASE) {
	return true;
})

/* BR-路由配置 */
.config(function($routeProvider) {
	$routeProvider.when('/welcome', {
		controller: WelcomeCtrl,
		templateUrl: 'pages/welcome.html'
	}).when('/index-home', {
		controller: IndexHomeCtrl,
		templateUrl: 'pages/index-home.html'
	}).when('/user-login', {
		controller: UserLoginCtrl,
		templateUrl: 'pages/user-login.html'
	}).when('/club-home', {
		controller: ClubHomeCtrl,
		templateUrl: 'pages/club-home.html'
	}).when('/club-propfee', {
		controller: ClubPropFeeCtrl,
		templateUrl: 'pages/club-propfee.html'
	}).when('/club-parklotfee', {
		controller: ClubParkLotFeeCtrl,
		templateUrl: 'pages/club-parklotfee.html'
	}).when('/club-askrepair', {
		controller: ClubAskRepairCtrl,
		templateUrl: 'pages/club-askrepair.html'
	}).when('/club-propmngt', {
		controller: ClubPropMngtCtrl,
		templateUrl: 'pages/club-propmngt.html'
	}).when('/club-parklotmngt', {
		controller: ClubParkLotMngtCtrl,
		templateUrl: 'pages/club-parklotmngt.html'
	}).when('/club-myworkflow', {
		controller: ClubMyWorkFlowCtrl,
		templateUrl: 'pages/club-myworkflow.html'
	}).when('/mine-home', {
		controller: MineHomeCtrl,
		templateUrl: 'pages/mine-home.html'
	}).when('/about-xwei', {
		controller: AboutXWeiCtrl,
		templateUrl: 'pages/about-xwei.html'
	}).when('/example', {
		controller: ExampleCtrl,
		templateUrl: 'pages/examples.html'
	}).otherwise({
		redirectTo: '/user-login'
	});
});
/* */
